home *** CD-ROM | disk | FTP | other *** search
/ PC/CD Gamer UK 120 / CD Gamer Issue 120 (March 2003) (Disc 2).ISO / mods / Q2_Codered / codeRED1_0.exe / Data1.cab / r_draw.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-13  |  9.0 KB  |  436 lines

  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20.  
  21. // r_draw.c
  22.  
  23. #include "r_local.h"
  24.  
  25. image_t        *draw_chars;
  26.  
  27. extern    qboolean    scrap_dirty;
  28. void Scrap_Upload (void);
  29.  
  30.  
  31. /*
  32. ===============
  33. Draw_InitLocal
  34. ===============
  35. */
  36. void Draw_InitLocal (void)
  37. {
  38.     // load console characters (don't bilerp characters)
  39.     draw_chars = GL_FindImage ("pics/conchars.pcx", it_pic);
  40.     GL_Bind( draw_chars->texnum );
  41.     qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  42.     qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  43. }
  44.  
  45.  
  46.  
  47. /*
  48. ================
  49. Draw_Char
  50.  
  51. Draws one 8*8 graphics character with 0 being transparent.
  52. It can be clipped to the top of the screen to allow the console to be
  53. smoothly scrolled off.
  54. ================
  55. */
  56. void Draw_Char (int x, int y, int num)
  57. {
  58.     int                row, col;
  59.     float            frow, fcol, size;
  60.  
  61.     num &= 255;
  62.     
  63.     if ( (num&127) == 32 )
  64.         return;        // space
  65.  
  66.     if (y <= -8)
  67.         return;            // totally off screen
  68.  
  69.     row = num>>4;
  70.     col = num&15;
  71.  
  72.     frow = row*0.0625;
  73.     fcol = col*0.0625;
  74.     size = 0.0625;
  75.  
  76.     GL_Bind (draw_chars->texnum);
  77.  
  78.     qglBegin (GL_QUADS);
  79.     qglTexCoord2f (fcol, frow);
  80.     qglVertex2f (x, y);
  81.     qglTexCoord2f (fcol + size, frow);
  82.     qglVertex2f (x+8, y);
  83.     qglTexCoord2f (fcol + size, frow + size);
  84.     qglVertex2f (x+8, y+8);
  85.     qglTexCoord2f (fcol, frow + size);
  86.     qglVertex2f (x, y+8);
  87.     qglEnd ();
  88. }
  89.  
  90. /*
  91. =============
  92. R_RegisterPic
  93. =============
  94. */
  95. image_t    *R_RegisterPic (char *name)
  96. {
  97.     image_t *gl;
  98.     char    fullname[MAX_QPATH];
  99.  
  100.     if (name[0] != '/' && name[0] != '\\')
  101.     {
  102.         Com_sprintf (fullname, sizeof(fullname), "pics/%s.pcx", name);
  103.         gl = GL_FindImage (fullname, it_pic);
  104.     }
  105.     else
  106.         gl = GL_FindImage (name+1, it_pic);
  107.  
  108.     return gl;
  109. }
  110.  
  111. /*
  112. =============
  113. R_RegisterPic
  114. =============
  115. */
  116. image_t    *R_RegisterParticlePic (char *name)
  117. {
  118.     image_t *gl;
  119.     char    fullname[MAX_QPATH];
  120.  
  121.     if (name[0] != '/' && name[0] != '\\')
  122.     {
  123.         Com_sprintf (fullname, sizeof(fullname), "particles/%s.tga", name);
  124.         gl = GL_FindImage (fullname, it_pic);
  125.     }
  126.     else
  127.         gl = GL_FindImage (name+1, it_pic);
  128.  
  129.     return gl;
  130. }
  131. /*
  132. =============
  133. Draw_GetPicSize
  134. =============
  135. */
  136. void Draw_GetPicSize (int *w, int *h, char *pic)
  137. {
  138.     image_t *gl;
  139.  
  140.     gl = R_RegisterPic (pic);
  141.     if (!gl)
  142.     {
  143.         *w = *h = -1;
  144.         return;
  145.     }
  146.     *w = gl->width;
  147.     *h = gl->height;
  148. }
  149.  
  150. /*
  151. =============
  152. Draw_StretchPic
  153. =============
  154. */
  155. void Draw_StretchPic (int x, int y, int w, int h, char *pic)
  156. {
  157.     image_t *gl;
  158.  
  159.     gl = R_RegisterPic (pic);
  160.     if (!gl)
  161.     {
  162.         Com_Printf ("Can't find pic: %s\n", pic);
  163.         return;
  164.     }
  165.  
  166.     if (scrap_dirty)
  167.         Scrap_Upload ();
  168.  
  169.     if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) ) && !gl->has_alpha)
  170.         qglDisable (GL_ALPHA_TEST);
  171.  
  172.     GL_Bind (gl->texnum);
  173.     qglBegin (GL_QUADS);
  174.     qglTexCoord2f (gl->sl, gl->tl);
  175.     qglVertex2f (x, y);
  176.     qglTexCoord2f (gl->sh, gl->tl);
  177.     qglVertex2f (x+w, y);
  178.     qglTexCoord2f (gl->sh, gl->th);
  179.     qglVertex2f (x+w, y+h);
  180.     qglTexCoord2f (gl->sl, gl->th);
  181.     qglVertex2f (x, y+h);
  182.     qglEnd ();
  183.  
  184.     if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) ) && !gl->has_alpha)
  185.         qglEnable (GL_ALPHA_TEST);
  186. }
  187.  
  188.  
  189. /*
  190. =============
  191. Draw_Pic
  192. =============
  193. */
  194. void Draw_Pic (int x, int y, char *pic)
  195. {
  196.     image_t *gl;
  197.  
  198.     gl = R_RegisterPic (pic);
  199.     if (!gl)
  200.     {
  201.         Com_Printf ("Can't find pic: %s\n", pic);
  202.         return;
  203.     }
  204.     if (scrap_dirty)
  205.         Scrap_Upload ();
  206.  
  207.     if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) ) && !gl->has_alpha)
  208.         qglDisable (GL_ALPHA_TEST);
  209.  
  210.     GL_Bind (gl->texnum);
  211.     qglBegin (GL_QUADS);
  212.     qglTexCoord2f (gl->sl, gl->tl);
  213.     qglVertex2f (x, y);
  214.     qglTexCoord2f (gl->sh, gl->tl);
  215.     qglVertex2f (x+gl->width, y);
  216.     qglTexCoord2f (gl->sh, gl->th);
  217.     qglVertex2f (x+gl->width, y+gl->height);
  218.     qglTexCoord2f (gl->sl, gl->th);
  219.     qglVertex2f (x, y+gl->height);
  220.     qglEnd ();
  221.  
  222.     if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) )  && !gl->has_alpha)
  223.         qglEnable (GL_ALPHA_TEST);
  224. }
  225.  
  226. /*
  227. =============
  228. Draw_TileClear
  229.  
  230. This repeats a 64*64 tile graphic to fill the screen around a sized down
  231. refresh window.
  232. =============
  233. */
  234. void Draw_TileClear (int x, int y, int w, int h, char *pic)
  235. {
  236.     image_t    *image;
  237.  
  238.     image = R_RegisterPic (pic);
  239.     if (!image)
  240.     {
  241.         Com_Printf ("Can't find pic: %s\n", pic);
  242.         return;
  243.     }
  244.  
  245.     if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) )  && !image->has_alpha)
  246.         qglDisable (GL_ALPHA_TEST);
  247.  
  248.     GL_Bind (image->texnum);
  249.     qglBegin (GL_QUADS);
  250.     qglTexCoord2f (x/64.0, y/64.0);
  251.     qglVertex2f (x, y);
  252.     qglTexCoord2f ( (x+w)/64.0, y/64.0);
  253.     qglVertex2f (x+w, y);
  254.     qglTexCoord2f ( (x+w)/64.0, (y+h)/64.0);
  255.     qglVertex2f (x+w, y+h);
  256.     qglTexCoord2f ( x/64.0, (y+h)/64.0 );
  257.     qglVertex2f (x, y+h);
  258.     qglEnd ();
  259.  
  260.     if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) )  && !image->has_alpha)
  261.         qglEnable (GL_ALPHA_TEST);
  262. }
  263.  
  264.  
  265. /*
  266. =============
  267. Draw_Fill
  268.  
  269. Fills a box of pixels with a single color
  270. =============
  271. */
  272. void Draw_Fill (int x, int y, int w, int h, int c)
  273. {
  274.     union
  275.     {
  276.         unsigned    c;
  277.         byte        v[4];
  278.     } color;
  279.  
  280.     if ( (unsigned)c > 255)
  281.         Com_Error (ERR_FATAL, "Draw_Fill: bad color");
  282.  
  283.     qglDisable (GL_TEXTURE_2D);
  284.  
  285.     color.c = d_8to24table[c];
  286.     qglColor3f (color.v[0]/255.0,
  287.         color.v[1]/255.0,
  288.         color.v[2]/255.0);
  289.  
  290.     qglBegin (GL_QUADS);
  291.  
  292.     qglVertex2f (x,y);
  293.     qglVertex2f (x+w, y);
  294.     qglVertex2f (x+w, y+h);
  295.     qglVertex2f (x, y+h);
  296.  
  297.     qglEnd ();
  298.     qglColor3f (1,1,1);
  299.     qglEnable (GL_TEXTURE_2D);
  300. }
  301.  
  302. //=============================================================================
  303.  
  304. /*
  305. ================
  306. Draw_FadeScreen
  307.  
  308. ================
  309. */
  310. void Draw_FadeScreen (void)
  311. {
  312.     qglEnable (GL_BLEND);
  313.     qglDisable (GL_TEXTURE_2D);
  314.     qglColor4f (0, 0, 0, 0.8);
  315.     qglBegin (GL_QUADS);
  316.  
  317.     qglVertex2f (0,0);
  318.     qglVertex2f (vid.width, 0);
  319.     qglVertex2f (vid.width, vid.height);
  320.     qglVertex2f (0, vid.height);
  321.  
  322.     qglEnd ();
  323.     qglColor4f (1,1,1,1);
  324.     qglEnable (GL_TEXTURE_2D);
  325.     qglDisable (GL_BLEND);
  326. }
  327.  
  328.  
  329. //====================================================================
  330.  
  331.  
  332. /*
  333. =============
  334. Draw_StretchRaw
  335. =============
  336. */
  337. extern unsigned    r_rawpalette[256];
  338.  
  339. void Draw_StretchRaw (int x, int y, int w, int h, int cols, int rows, byte *data)
  340. {
  341.     unsigned    image32[256*256];
  342.     unsigned char image8[256*256];
  343.     int            i, j, trows;
  344.     byte        *source;
  345.     int            frac, fracstep;
  346.     float        hscale;
  347.     int            row;
  348.     float        t;
  349.  
  350.     GL_Bind (0);
  351.  
  352.     if (rows<=256)
  353.     {
  354.         hscale = 1;
  355.         trows = rows;
  356.     }
  357.     else
  358.     {
  359.         hscale = rows/256.0;
  360.         trows = 256;
  361.     }
  362.     t = rows*hscale / 256;
  363.  
  364.     if ( !qglColorTableEXT )
  365.     {
  366.         unsigned *dest;
  367.  
  368.         for (i=0 ; i<trows ; i++)
  369.         {
  370.             row = (int)(i*hscale);
  371.             if (row > rows)
  372.                 break;
  373.             source = data + cols*row;
  374.             dest = &image32[i*256];
  375.             fracstep = cols*0x10000/256;
  376.             frac = fracstep >> 1;
  377.             for (j=0 ; j<256 ; j++)
  378.             {
  379.                 dest[j] = r_rawpalette[source[frac>>16]];
  380.                 frac += fracstep;
  381.             }
  382.         }
  383.  
  384.         qglTexImage2D (GL_TEXTURE_2D, 0, gl_tex_solid_format, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, image32);
  385.     }
  386.     else
  387.     {
  388.         unsigned char *dest;
  389.  
  390.         for (i=0 ; i<trows ; i++)
  391.         {
  392.             row = (int)(i*hscale);
  393.             if (row > rows)
  394.                 break;
  395.             source = data + cols*row;
  396.             dest = &image8[i*256];
  397.             fracstep = cols*0x10000/256;
  398.             frac = fracstep >> 1;
  399.             for (j=0 ; j<256 ; j++)
  400.             {
  401.                 dest[j] = source[frac>>16];
  402.                 frac += fracstep;
  403.             }
  404.         }
  405.  
  406.         qglTexImage2D( GL_TEXTURE_2D, 
  407.                        0, 
  408.                        GL_COLOR_INDEX8_EXT, 
  409.                        256, 256, 
  410.                        0, 
  411.                        GL_COLOR_INDEX, 
  412.                        GL_UNSIGNED_BYTE, 
  413.                        image8 );
  414.     }
  415.     qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  416.     qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  417.  
  418.     if ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) ) 
  419.         qglDisable (GL_ALPHA_TEST);
  420.  
  421.     qglBegin (GL_QUADS);
  422.     qglTexCoord2f (0, 0);
  423.     qglVertex2f (x, y);
  424.     qglTexCoord2f (1, 0);
  425.     qglVertex2f (x+w, y);
  426.     qglTexCoord2f (1, t);
  427.     qglVertex2f (x+w, y+h);
  428.     qglTexCoord2f (0, t);
  429.     qglVertex2f (x, y+h);
  430.     qglEnd ();
  431.  
  432.     if ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) ) 
  433.         qglEnable (GL_ALPHA_TEST);
  434. }
  435.  
  436.